// pages/carPhotos/carPhotos.js
var api = require('../../utils/api.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
      page_star:0,
      listData:[],

    hideMore:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    //进来查询缓存记录
    let deli_data = wx.getStorageSync('delivery');
    if (deli_data) {
      let page_star = wx.getStorageSync('page_star');
      that.setData({
        page_star: page_star,
        listData: deli_data,
        hideMore: false
      });

    } else {  //重新获取
      that.setData({
        page_star:0
      });
      console.log('重新获取')
      that.getDelivery();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page_star: 0
    });
      this.getDelivery();

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  ///预览图片
  previewImage: function (e) {
      var current = e.target.dataset.src;
      var id = e.target.id

      //预览列表
      var viewList = this.data.listData;
      var aaa =  viewList[id].delivery_pic

      wx.previewImage({
          current: current, // 当前显示图片的http链接  
          urls: viewList[id].delivery_pic // 需要预览的图片http链接列表  
      })
  },
  /**
   * 获取交车列表
   */
  getDelivery:function(){
    let that = this;
    let page_star = that.data.page_star;

    api.post({
      url: 'portal/delivery/index',
      data: { page_star: page_star},
      success: function (data) {
        console.log(data)
        if (data.code) {
          let listData = that.data.listData;
          if (page_star == 0 ){
            listData = data.data;
            page_star = data.data.length;
          }else{
            //原有数据
            listData = listData.concat(data.data);
            page_star += data.data.length;
          }

          let hideMore = true;

          //判断是否显示“更多”
          if (data.data.length == 10){
            hideMore = false;
          }

          that.setData({
            listData: listData,
            page_star: page_star,
            hideMore: hideMore
          })

          wx.setStorage({
            key: "delivery",
            data: listData
          })
          wx.setStorage({
            key: "page_star",
            data: page_star
          })

        }else{
          wx.showToast({
            title: data.msg,
            icon: 'error',
            duration: 1000
          });
        }
        
      },
      fail: function (res) {

      }
    })
  },

  /**
   * 获取更多
   */
  getMore:function(){
    this.getDelivery();
  }
})